<template>
  <div class="event-detail-box" v-loading="loading">
    <div class="bottom-info" style="height: calc(100% - 20px)">
      <div class="left-info">
        <div class="video-box">
          <div style="height: 350px">
            <videoRtsp
              v-if="
                currentInfo.cameraIndexCode && !showpreve && !showDigVisable
              "
              :Times="currentInfo.reportTime"
              :vid="currentInfo.cameraIndexCode"
            >
            </videoRtsp>
            <div class="border-top" :style="borderStop"></div>
          </div>
        </div>
        <!-- 详细信息 -->
        <div class="baseinfo">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="事件信息" name="base">
              <p class="textinfo">事件类型：{{ currentInfo.eventCodeDesc }}</p>
              <p class="textinfo">上报时间：{{ currentInfo.reportTime }}</p>
              <p class="textinfo">事件等级：{{ currentInfo.eventClassDesc }}</p>
              <p class="textinfo">
                所属社区：{{ currentInfo.maintainOrgName }}
              </p>
              <p class="textinfo">报警点位：{{ currentInfo.address }}</p>
              <p class="textinfo">
                点位编号：{{ currentInfo.cameraIndexCode }}
              </p>
              <p class="textinfo">事件地址：{{ currentInfo.address }}</p>
               <template v-if="currentInfo.sensorInfo">
                <p class="textinfo">
                  当前值：{{ currentInfo.sensorInfo.curValue }}
                </p>
                <p class="textinfo">
                  报警阀值：{{ currentInfo.sensorInfo.thresholdValue }}
                </p>
                <p class="textinfo">
                  传感器编号：{{ currentInfo.sensorInfo.sensorId }}
                </p>
                <p class="textinfo">
                  传感器类型：{{ currentInfo.sensorInfo.sensorName }}
                </p>
              </template>
              <p class="textinfo">
                抓拍照片：<img
                  :src="currentInfo.pic"
                  @click="showImg(currentInfo.pic)"
                />
              </p>
              <div
                class="textinfo"
                v-if="
                  currentInfo.eventCode &&
                  (currentInfo.eventCode.indexOf('1001') > -1 ||
                    currentInfo.eventCode.indexOf('1002') > -1)
                "
              >
                录像回放：
                <div @click="showbk">
                  <videoplayBk :url="videoUrl"></videoplayBk>
                </div>
              </div>
              <!-- <p class="playbackbtn" @click="showbk">录像回放</p> -->
              <!-- <p class="labeltitle">检测目标</p>
              <span
                class="eyebtn"
                v-for="(item, index) in snapList"
                :key="index"
                @click="selectBorder(index, item)"
              >
                目标{{ index + 1 }}
                <img
                  v-if="currentBorder == index"
                  src="../../assets/images/open.png"
                />
                <img v-else src="../../assets/images/close.png" />
              </span> -->
            </el-tab-pane>
            <!-- <el-tab-pane label="规则信息" name="gz"></el-tab-pane> -->
          </el-tabs>
        </div>
        <div class="zj-info">
          <!-- <div class="confirm-btn" @click="save">一键取证</div> -->
          <div class="img-list">
            <!-- <div
              class="box"
              v-for="(item, index) in downlist.slice(0, 2)"
              :key="index"
            >
              <img class="img" :src="item.url" />
              <p class="name">{{ item.name }}</p>
            </div> -->
            <div class="box" v-show="downlist.length > 2">
              <videoplayBk
                :capUrl="downlist.length > 1 && downlist[1].url ? false : true"
                @capImg="capImgData"
                :url="downlist.length > 2 ? downlist[2].url : ''"
              ></videoplayBk>
            </div>
          </div>
        </div>
      </div>

      <div class="right-info" v-if="currentInfo.status === '0'">
        <div class="detail-content">
          <div class="header-zp">研判意见</div>
          <p class="line"></p>
          <el-form
            ref="ruleFormRef"
            :model="formValue"
            label-width="auto"
            :rules="rules"
            class="ruleForm"
          >
            <el-form-item label="研判结果" prop="judgeResult">
              <el-radio-group v-model="formValue.judgeResult">
                <el-radio :value="1">正报</el-radio>
                <el-radio :value="2">误报</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="事件等级" prop="eventClass">
              <el-select
                v-model="formValue.eventClass"
                placeholder="请选择"
                clearable
                filterable
              >
                <el-option
                  v-for="item in typeState.sjdj"
                  :key="item.value"
                  :label="item.key"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="研判备注" prop="judgeDesc">
              <el-input
                v-model="formValue.judgeDesc"
                :rows="4"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item>
          </el-form>
          <!-- <el-button @click="saveForm">提交</el-button> -->
          <div class="confirm-btn notips" @click="saveForm">提交</div>
        </div>
      </div>
      <div
        v-if="currentInfo.status == '1' || currentInfo.status == '2'"
        class="right-info"
      >
        <div class="detail-content">
          <div class="header-zp">研判意见</div>
          <p class="line"></p>
          <el-form
            ref="ruleFormRef"
            :model="formValue"
            label-width="auto"
            class="ruleForm"
          >
            <el-form-item label="研判结果：" prop="status">
              <span> {{ currentInfo.status == "1" ? "正报" : "误报" }}</span>
            </el-form-item>
            <el-form-item label="事件等级：" prop="eventClassDesc">
              {{
                currentInfo.eventClassDesc ? currentInfo.eventClassDesc : "-"
              }}
            </el-form-item>
            <el-form-item label="研判备注：" prop="judgeComment">
              {{ currentInfo.judgeComment ? currentInfo.judgeComment : "-" }}
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!-- 视频播放 -->
    <SelfDialog
      style="width: 600px"
      :dialogVisable="showDigVisable"
      title="视频播放"
      @close="showDigVisable = false"
      :showFooter="false"
      :isright="false"
    >
      <template #default>
        <div style="width: 400px; height: 400px; margin: 20px auto">
          <videoplayBk class="bigbk" v-if="showDigVisable" :url="videoUrl"></videoplayBk>
        </div>
      </template>
    </SelfDialog>
    <vue-easy-lightbox
      teleport-to="body"
      :visible="showpreve"
      :index="0"
      :imgs="urlCatch"
      class="lightbox-override"
      @hide="showpreve = false"
    />
  </div>
</template>

<script setup>
import zp from "./zp.vue";
import cz from "./cz.vue";
import videoRtsp from "@/components/video/videoRtsp.vue";
import videoplay from "@/components/video/video.vue";
import videoplayBk from "@/components/video/videobk.vue";
import { getyanqianxiangqingData, tijiaoxiangqingData } from "@/api/zhzx.js";
import { getOneKeyCheck } from "@/api/event.js";
import { onMounted, onUnmounted } from "vue";
import { useRoute } from "vue-router";
import { Minxfun } from "@/utils/minxin.js";
import VueEasyLightbox from "vue-easy-lightbox";
const { showImg, urlCatch, showpreve } = Minxfun();
import axios from "axios";
const route = useRoute();
const activeName = ref("base");
import { useUserStore } from "@/pinia/user.js";
const piniaUser = useUserStore();
import { TypeMin } from "@/utils/type.js";

const { typeState } = TypeMin();

let timer = null;

onUnmounted(() => {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
});

let currentInfo = ref({ id: "1" });

let snapList = ref([
  {
    x: "0.1",
    y: "0.2",
    w: "0.2",
    h: "0.3",
  },
]);

let currentBorder = ref(-1);
let borderStop = ref({
  width: 0,
  height: 0,
  opacity: 0,
});
// 选择border
const selectBorder = (index, item) => {
  if (currentBorder.value == index) {
    currentBorder.value = -1;
    borderStop.value = {
      width: 0,
      height: 0,
      opacity: 0,
    };
  } else {
    currentBorder.value = index;
    borderStop.value = {
      width: 640 * item.w + "px",
      height: 360 * item.h + "px",
      left: 640 * item.x + "px",
      top: 360 * item.y + "px",
    };

    console.log(borderStop.value);
  }
};

// 下载

let downlist = ref([]);

const save = async () => {
  try {
    let { data } = await getOneKeyCheck({
      busizEventId: route.query.busizEventId,
      role: localStorage.getItem("User"),
      deviceId: currentInfo.value.deviceId,
      uuid: "",
    });
    if (!data.playBackVideoUrl) return;
    downlist.value = [
      {
        name: "远景照片",
        url: data.distantViewPicUrl,
      },
      {
        name: "近景照片",
        url: data.closeShotUrl,
      },
      {
        name: "录像",
        url: data.playBackVideoUrl,
      },
    ];
  } catch (e) {
    console.error(e);
  }
};
let eventInfo = ref({});
// 获取案件基本信息
const getDetailData = async () => {
  try {
    const { data } = await getyanqianxiangqingData({
      id: route.query.busizEventId,
    });
    currentInfo.value = data || {};
    formValue.value.eventClass = currentInfo.value.eventClass;
    formValue.value.eventClassDesc = currentInfo.value.eventClassDesc;
    console.log("currentInfo.value)", currentInfo.value);
    if (
      data.eventCode &&
      (data.eventCode.indexOf("1001") > -1 ||
        data.eventCode.indexOf("1002") > -1)
    ) {
      getshowbk();
    }
  } catch (error) {
    console.error(error);
  }
};

function base64ToFile(base64, filename, mimeType) {
  // 将Base64字符串转换为二进制数据
  const byteCharacters = atob(base64.split(",")[1]);
  const byteNumbers = new Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  const byteArray = new Uint8Array(byteNumbers);

  // 创建Blob对象
  const blob = new Blob([byteArray], { type: mimeType });

  // 创建File对象
  const file = new File([blob], filename, { type: mimeType });

  return file;
}

let showDigVisable = ref(false);
let videoUrl = ref("");

// 获取回放url
const getshowbk = async () => {
  try {
    const { data } = await getOneKeyCheck({
      busizEventId: route.query.busizEventId,
      role: localStorage.getItem("User"),
      deviceId: currentInfo.value.deviceId,
      uuid: "",
      previewTimeDurning: 60000,
      postTimeDurning: 60000,
    });
    if (!data.playBackVideoUrl) return;
    nextTick(() => {
      videoUrl.value = data.playBackVideoUrl;
    });
  } catch (e) {
    console.error(e);
  }
};
// 展示回放
const showbk = async () => {
  try {
    const { data } = await getOneKeyCheck({
      busizEventId: route.query.busizEventId,
      role: localStorage.getItem("User"),
      deviceId: currentInfo.value.deviceId,
      uuid: "",
      previewTimeDurning: 60000,
      postTimeDurning: 60000,
    });
    if (!data.playBackVideoUrl) return;

    showDigVisable.value = true;

    nextTick(() => {
      videoUrl.value = data.playBackVideoUrl;
    });
  } catch (e) {
    console.error(e);
  }
};

// 截图
const capImgData = async (base) => {
  try {
    if (!base) return;
    let file = await base64ToFile(base, "小图.png", "PNG");
    const formData = new FormData();
    formData.append("file", file);
    formData.append("busizEventId", route.query.busizEventId);
    let { data } = await axios.post(
      "/integrated-workbench/v1/enforcement/picuploadwithbusizeventid",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    );
    downlist.value[1] = {
      name: "近景图片",
      url: data.data.picPath,
    };
  } catch (e) {
    console.error(e);
  }
};

let formValue = ref({
  id: "",
  judgeResult: 1, //1 正报 2 误报
  eventClass: "", //来自下拉选择，不选则为空
  judgeDesc: "",
});
let rules = reactive({
  judgeResult: [
    { required: true, message: "请选择", trigger: "change", type: Number },
  ],
  eventClass: [{ required: true, message: "请选择", trigger: "change" }],
  // judgeDesc: [{ required: true, message: '请输入备注', trigger: 'blur' }],
});
let ruleFormRef = ref();
let loading = ref(false);
const saveForm = async () => {
  try {
    ruleFormRef.value.validate(async (valid, fields) => {
      if (!valid) return;
      loading.value = true;
      let obj = {
        ...formValue.value,
        id: currentInfo.value.id,
      };
      let { data } = await tijiaoxiangqingData(obj);
      loading.value = false;
      getDetailData();
    });
  } catch (e) {
    console.error(e);
  }
};

onMounted(() => {
  getDetailData();
});
</script>

<style lang="scss">
.event-detail-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .confirm-btn {
    width: 143px;
    height: 36px;
    background: #2263bf;
    border-radius: 4px;
    font-size: 12px;
    color: #ffffff;
    line-height: 36px;
    cursor: pointer;
    text-align: center;
    position: relative;
    &.notips {
      // margin: 20px auto;
      margin-left: 78px;
      &::after {
        content: "";
      }
    }
    &::after {
      width: 300px;
      content: "默认下载内容：事发前后10s视频，事件截图";
      position: absolute;
      right: -280px;
      top: 0px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 12px;
      color: #999999;
    }
  }
  .step-top-info {
    width: 100%;
    height: 170px;
    background: #ffffff;
    box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
    border-radius: 12px;
    display: flex;
    align-items: top;
    padding: 20px 50px;
    .step-box {
      width: 25%;
      height: 100%;
      margin-right: 30px;
      &:nth-child(4) {
        margin-right: 0;
      }
      .header-step {
        display: flex;
        align-items: center;
        .step-num {
          display: inline-block;
          width: 32px;
          height: 32px;
          background: #2263bf;
          text-align: center;
          line-height: 32px;
          color: #ffffff;
          margin-right: 17px;
          border-radius: 50%;
          &.normal {
            background: rgba(142, 191, 255, 0.72);
            border: 1px solid #2c84ff;
            color: #2263bf;
          }
        }
        .step-img {
          display: inline-block;
          width: 32px;
          height: 32px;
          margin-right: 17px;
        }
        .step-title {
          display: inline-block;
          width: 71px;
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 16px;
          color: #333333;
          line-height: 22px;
          margin-right: 40px;
        }
        .line {
          flex: 1;
          height: 1px;
          background: rgba(34, 99, 191, 0.2);
        }
      }
      .step-content {
        padding-left: 50px;
        .info-text {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #999999;
          line-height: 25px;
        }
      }
    }
  }
  .bottom-info {
    width: 100%;
    height: calc(100% - 198px);
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    .left-info {
      flex: 1;
      height: 100%;
      background: #ffffff;
      box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
      border-radius: 6px;
      padding: 36px 25px 0 35px;
      .video-box {
        display: inline-block;
        vertical-align: top;
        width: 640px;
        height: 350px;
        position: relative;

        .border-top {
          position: absolute;
          border: 1px solid red;
        }
      }
      .baseinfo {
        display: inline-block;
        vertical-align: top;
        width: calc(100% - 700px);
        margin-left: 57px;
        .textinfo {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
          line-height: 29px;
          text-align: left;
          margin-bottom: 10px;
          display: flex;
          align-items: top;
          img {
            cursor: pointer;
            width: 146px;
            height: 82px;
          }
        }
        .labeltitle {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 16px;
          margin-top: 18px;
          margin-bottom: 18px;
        }
        .eyebtn {
          width: 128px;
          height: 40px;
          border: 1px solid #eeeeee;
          margin-right: 10px;
          line-height: 40px;
          display: flex;
          align-items: center;
          justify-content: space-around;
          cursor: pointer;
          padding: 0 12px;
          img {
          }
        }
      }
      .video-cover {
        width: 100%;
        height: 40px;
        background: rgba(142, 191, 255, 0.22);
        display: flex;
        align-items: center;
        justify-content: space-around;
        line-height: 40px;
        .btnv {
          display: flex;
          align-items: center;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
          cursor: pointer;
          img {
            margin-right: 11px;
          }
        }
      }
    }
    .right-info {
      width: 520px;
      height: 100%;
      background: #ffffff;
      box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
      border-radius: 6px;
      margin-left: 20px;
    }
  }
  .zj-info {
    width: 640px;
    height: 200px;
    margin-top: -150px;
    overflow: auto;
    position: relative;
    .down {
      position: absolute;
      right: 0;
      top: 10px;
      display: flex;
      align-items: center;
      cursor: pointer;
      font-size: 14px;
      color: #2263bf;
      img {
        margin-right: 10px;
      }
    }

    .img-list {
      margin-top: 15px;
      display: flex;
      align-items: center;
      .box {
        width: 200px;
        height: 117px;
        margin-right: 8px;
        .img {
          width: 200px;
          height: 100px;
        }
        .name {
          text-align: center;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
        }
      }
    }
  }
}
.el-tabs__nav-wrap:after {
  height: 1px !important;
  background: rgba(34, 99, 191, 0.2) !important;
}
.el-tabs__active-bar {
  background: #2263bf !important;
}
.el-tabs__item {
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: #333333;
}
.el-tabs__item.is-active,
.el-tabs__item:hover {
  color: #2263bf !important;
}
.el-tab-pane {
  padding-left: 10px;
}

.video-dialog {
  padding-left: 20px;
  //   width: 531px;
  height: 100%;
  display: inline-block;
  vertical-align: top;
  position: absolute;
  left: -420px;
  top: 0px;
}
.right-yzinfo {
  display: inline-block;
  vertical-align: top;
  width: 330px;
  height: 100%;
  padding-top: 40px;
  margin-left: 40px;
  .yzimg {
    margin-left: 100px;
    margin-bottom: 18px;
    width: 50px;
    height: 50px;
  }
  .text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 24px;
  }
  .panel-btn {
    display: flex;
    align-items: center;
    justify-content: space-around;
    cursor: pointer;

    .btn-l {
      width: 45%;
      display: flex;
      align-items: center;
      color: #2263bf;
      img {
        margin-right: 10px;
      }
    }
    .btn-r {
      width: 40%;
      height: 36px;
      background: #2263bf;
      border-radius: 4px;
      font-size: 12px;
      color: #ffffff;
      line-height: 36px;
      cursor: pointer;
      text-align: center;
    }
  }
}
</style>

<style lang="scss" scoped>
.detail-content {
  .header-zp {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 16px;
    color: #333333;
    line-height: 22px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .eventbtn {
      width: 150px;
      height: 36px;
      background: #ffb81a;
      border-radius: 3px;
      font-size: 14px;
      color: #ffffff;
      line-height: 36px;
      display: flex;
      align-items: center;
      justify-content: space-around;
      cursor: pointer;
    }
  }
  .line {
    width: 100%;
    height: 1px;
    margin: 18px 0;
    background: rgba(34, 99, 191, 0.2);
  }
  padding: 30px 40px;
  .detail-title {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 14px;
    color: #333333;
    line-height: 20px;
    margin-bottom: 11px;
  }
  .text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 24px;
  }
  .img-list {
    display: flex;
    align-items: center;
    justify-content: space-between;
    img {
      width: 31%;
      height: 100px;
    }
  }
}

.center_content-report {
  padding: 30px 40px;
  .detail-title {
    font-family: PingFangSC, PingFang SC;
    font-weight: 600;
    font-size: 14px;
    color: #333333;
    line-height: 20px;
    margin-bottom: 11px;
  }
  .text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 24px;
  }
  .img-list {
    display: flex;
    align-items: center;
    justify-content: space-between;
    img {
      width: 31%;
      height: 100px;
    }
  }
  .panel-btn {
    width: 100%;
    display: flex;
    align-items: center;
    text-align: center;
    .btn {
      margin: 0 auto;
      width: 100px;
      height: 36px;
      background: #ffffff;
      border-radius: 3px;
      line-height: 36px;
      text-align: center;
      cursor: pointer;
      color: #4e97ff;
      &:nth-child(2) {
        width: 100px;
        height: 36px;
        background: #4e97ff;
        border-radius: 3px;
        color: #fff;
        line-height: 36px;
        text-align: center;
        cursor: pointer;
      }
    }
  }
}
</style>
